package com.lyf.scm.admin.api.controller;


import com.lyf.scm.admin.api.dto.DirectlyExecuteDTO;
import com.lyf.scm.admin.domain.service.DirectlyExecuteService;
import com.lyf.scm.common.model.ExcelExportDTO;
import com.lyf.scm.common.util.ExcelExportUtil;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.util.controller.RomeController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * description:
 *          直营预测计划--执行
 * @author: haodong
 * @data: 2020/8/5 13:29
 */
@Slf4j
@Api(tags = "直营预测计划--执行")
@RequestMapping("/api/v1/salesPlanDirectlyExecute")
@RomeController
public class DirectlyExecuteController {

    @Resource
    private DirectlyExecuteService directlyExecuteService;

    @PostMapping("/list")
    @ApiOperation(value = "按条件查询直营预测计划(天维度)")
    public Response listBySelective(@RequestBody DirectlyExecuteDTO salesPlanDirectlyExecute) {
        return Response.builderSuccess(directlyExecuteService.listBySelective(salesPlanDirectlyExecute));
    }

    @PostMapping("/listAll")
    @ApiOperation(value = "查询全渠道直营预测计划(天维度)")
    public Response listAllByDays(@RequestBody DirectlyExecuteDTO salesPlanDirectlyExecute) {
        return Response.builderSuccess(directlyExecuteService.listAllByDays(salesPlanDirectlyExecute));
    }

    @PostMapping("/exportExcel")
    @ApiOperation("导出Excel")
    public void exportExcel(@RequestBody ExcelExportDTO excelExportDTO, @RequestParam Integer type, HttpServletResponse httpServletResponse) {
        try {
            //获得feign响应流对象
            feign.Response response = directlyExecuteService.exportExcel(excelExportDTO, type);
            //如果返回普通json对象，处理异常响应
            if (ExcelExportUtil.validJsonResponse(response.headers().get(ExcelExportUtil.HEAD_CONTENT_TYPE))) {
                ExcelExportUtil.handleProviderErrorResponse(response, httpServletResponse);
            }
            //如果返回excel对象，处理正确响应
            if (ExcelExportUtil.validExcelResponse(response.headers().get(ExcelExportUtil.HEAD_CONTENT_TYPE))) {
                String sourceFileName = "直营预测计划——执行";
                ExcelExportUtil.handleProviderRightResponse(response, httpServletResponse, sourceFileName);
            }
        } catch (Exception e) {
            ExcelExportUtil.handleExceptionResponse(httpServletResponse, e);
        }
    }
}
